package ysq.xyg.web.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.model.po.*;
import ysq.xyg.host.model.vo.*;
import ysq.xyg.host.service.*;
import ysq.xyg.host.util.HostBaseContent;
import ysq.xyg.web.model.po.*;
import ysq.xyg.web.model.vo.*;
import ysq.xyg.web.service.*;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网站端
 *
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@Slf4j
@RestController
public class PcController {
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebNavigationService webNavigationService;
    @Autowired
    private WebCheckCodeService webCheckCodeService;
    @Autowired
    private HostItemService hostItemService;
    @Autowired
    private HostTypeService hostTypeService;
    @Autowired
    private WebProductOrderService webProductOrderService;
    @Autowired
    private WebScoreRecordService webScoreRecordService;
    @Autowired
    private WebMemberService webMemberService;
    @Autowired
    private HostSearchService hostSearchService;
    @Autowired
    private WebArticleService webArticleService;
    @Autowired
    private HostWithdrawService hostWithdrawService;
    @Autowired
    private WebShareService webShareService;
    @Autowired
    private WebBannerService webBannerService;
    @Autowired
    private WebColumnTypeService webColumnTypeService;
    @Autowired
    private WebProductService webProductService;
    @Autowired
    private WebUnionidService webUnionidService;
    @Autowired
    private HostAreaService hostAreaService;
    @Autowired
    private HostCityService hostCityService;
    @Autowired
    private HostDetailService hostDetailService;
    @Autowired
    private HostReportService hostReportService;
    @Autowired
    private HostAdviseService hostAdviseService;
    @Autowired
    private HostSettledService hostSettledService;
    @Autowired
    private WebLinkerService webLinkerService;

    private ModelAndView checkModelAndView;
    /**
     * 首页
     * */
    @RequestMapping("/")
    public ModelAndView pc(){
        return new ModelAndView("redirect:/webPc/index");
    }

    /**
     * 首页
     * */
    @RequestMapping("/webPc/index")
    public ModelAndView index(HttpServletRequest request,String cityId,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        List<HostCity> hostCityList=hostCityService.acceptList(new HostCityVo());
        List<HostType> hostTypeList=hostTypeService.acceptList(new HostTypeVo());
        ModelAndView modelAndView = new ModelAndView("/site/index");
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("hostCityList", hostCityList);
        //设置当前城市
        if(hostCityList!=null&&hostCityList.size()>0){
            if(StrUtil.isEmpty(cityId)){
                cityId=hostCityList.get(0).getId();
                modelAndView.addObject("currentCity", hostCityList.get(0));
            }else{
                modelAndView.addObject("currentCity", hostCityService.selectByPrimaryKey(cityId));
            }
            modelAndView.addObject("webBannerList",webBannerService.acceptList(new WebBannerVo(WebBaseContent.WEB_BANNNER_TYPE_SY,BaseConstant.base_use_type_moblie,hostCityList.get(0).getId())));
        }
        //设置当前房屋类型
        if(hostTypeList!=null&&hostTypeList.size()>0){
            modelAndView.addObject("currentHostType", hostTypeList.get(0));
        }
        modelAndView.addObject("hostTypeList",hostTypeList);
        modelAndView.addObject("cityId",cityId);
        //设置热卖
        List<HostItem> hostItemList= hostItemService.listLeft(new HostItemVo(cityId, HostBaseContent.HOST_HOT_SALE_STATE_KQ));
        modelAndView.addObject("hostItemList",hostItemList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }

    /**
     * 列表
     * */
    @RequestMapping("/webPc/list")
    public ModelAndView list(HttpServletRequest request,String cityId,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView = new ModelAndView("/site/list");
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        List<HostCity> hostCityList=hostCityService.acceptList(new HostCityVo());
        //设置当前城市
        if(hostCityList!=null&&hostCityList.size()>0){
            if(StrUtil.isEmpty(cityId)){
                cityId=hostCityList.get(0).getId();
                modelAndView.addObject("cityId", hostCityList.get(0).getId());
                modelAndView.addObject("currentCity", hostCityList.get(0));
            }else{
                modelAndView.addObject("currentCity",hostCityService.selectByPrimaryKey(cityId));
                modelAndView.addObject("cityId",cityId);
            }
        }
        modelAndView.addObject("hostCityList", hostCityList);
        //获取区域
        List<HostArea> hostAreaList=hostAreaService.acceptList(new HostAreaVo(cityId));
        if(hostAreaList!=null&&hostAreaList.size()>0){
            modelAndView.addObject("currentArea", hostAreaList.get(0));
            modelAndView.addObject("hostAreaList",hostAreaList);
        }
        List<HostType> hostTypeList=hostTypeService.acceptList(new HostTypeVo());
        //设置当前房屋类型
        if(hostTypeList!=null&&hostTypeList.size()>0){
            modelAndView.addObject("currentHostType", hostTypeList.get(0));
        }
        //设置热卖
        List<HostItem> hostItemList= hostItemService.listLeft(new HostItemVo(cityId, HostBaseContent.HOST_HOT_SALE_STATE_KQ));
        modelAndView.addObject("hostItemList",hostItemList);
        modelAndView.addObject("hostTypeList",hostTypeList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }
    /**
     * 加入会员
     * */
    @RequestMapping("/webPc/member")
    public ModelAndView member(HttpServletRequest request,String cityId,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView = new ModelAndView("/site/member");
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("cityId",cityId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }
    /**
     * 商品详情
     * */
    @RequestMapping("/webPc/item")
    public ModelAndView item(HttpServletRequest request,String id,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView = new ModelAndView("/site/item");
        HostItem hostItem= hostItemService.selectLeftObject(new HostItemVo(id));
        List<HostSettled> hostSettledList=hostSettledService.listLeft(new HostSettledVo(hostItem.getId(),"",BaseConstant.BASE_CHECK_STATUS_YSH));
        modelAndView.addObject("hostItem",hostItem);
        modelAndView.addObject("hostSettledList", hostSettledList);
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }
    /**
     * 积分商城
     * */
    @RequestMapping("/webPc/score")
    public ModelAndView score(HttpServletRequest request,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView = new ModelAndView("/site/score");
        List<WebProduct> webProductList=webProductService.acceptList(new WebProductVo(BaseConstant.base_show_type_zs,BaseConstant.base_show_type_zs));
        List<WebColumnType> webColumnTypeList=webColumnTypeService.acceptList(new WebColumnTypeVo(WebBaseContent.web_col_type_product));
        modelAndView.addObject("webColumnTypeList", webColumnTypeList);
        modelAndView.addObject("webProductList", webProductList);
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }
    /**
     * 关于我们
     * */
    @RequestMapping("/webPc/about")
    public ModelAndView about(HttpServletRequest request,String cityId,String navId) throws IOException {
        WebConfig webConfig=acceptWebConfig();
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView = new ModelAndView("/site/about");
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("navId",navId);
        modelAndView.addObject("cityId",cityId);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webLinkerList",webLinkerService.acceptList(new WebLinkerVo("5",BaseConstant.base_show_type_zs)));
        return modelAndView;
    }

    /**
     * 友情链接
     *
     * @param webLinkerVo 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    @RequestMapping(value = "/webPc/linker",method = RequestMethod.POST)
    public ReturnEntity linkerList(WebLinkerVo webLinkerVo){
        webLinkerVo.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        webLinkerVo.setShowType(BaseConstant.base_show_type_zs);
        return new ReturnEntity(webLinkerService.acceptList(webLinkerVo));
    }

    public WebConfig acceptWebConfig(){
        List<WebConfig> list=webConfigService.acceptList(new WebConfigVo());
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
